package com.ophiux.util;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import javax.imageio.ImageIO;

public class WaterMarkUtil {

	/**
	 * 
	 * Description:给图片添加icon水印 <br>
	 * 创 建 人: xgy 创建日期:2019年2月27日 下午4:32:22
	 * 
	 * @param srcImgPath
	 *            源文件
	 * @param iconImpPath
	 *            图标文件
	 * @param outImgPath
	 *            输出文件
	 * @return
	 */
	public static boolean mark(String srcImgPath, String iconImpPath, String outImgPath) {
		FileOutputStream outImgStream = null;
		boolean success = true;
		ImageObserver observer = null;
		String formatName = null;
		try {
			// 读取原图片信息
			File srcImgFile = new File(srcImgPath);
			File iconImgFile = new File(iconImpPath);

			formatName = srcImgPath.substring(srcImgPath.lastIndexOf(".") + 1, srcImgPath.length());

			Image srcImg = ImageIO.read(srcImgFile);
			Image icon = ImageIO.read(iconImgFile);

			int srcImgWidth = srcImg.getWidth(null);
			int srcImgHeight = srcImg.getHeight(null);

			BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);

			Graphics2D g = bufImg.createGraphics();
			g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);

			g.drawImage(icon, srcImgWidth - icon.getWidth(null) - 100, srcImgHeight - icon.getHeight(null) - 100,
					icon.getWidth(null), icon.getHeight(null), observer);
			g.dispose();

			outImgStream = new FileOutputStream(outImgPath);
			ImageIO.write(bufImg, formatName, outImgStream);

			outImgStream.flush();
			outImgStream.close();
		} catch (Exception e) {
			e.printStackTrace();
			success = false;
		} finally {
			if (outImgStream != null) {
				try {
					outImgStream.close();

				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return success;
	}

	public static void main(String[] args) throws IOException {
		String srcImgPath = "C:\\Users\\Administrator\\Desktop\\1.jpg";
		String icoImpPath = "C:\\\\Users\\\\Administrator\\\\Desktop\\\\xzy.png";
		String outImgPath = "C:\\\\Users\\\\Administrator\\\\Desktop\\\\xzy1.png";

		 mark(srcImgPath, icoImpPath, outImgPath);
		String url = "http://pic.sogou.com/pics/recompic/detail.jsp?category=%E7%BE%8E%E5%A5%B3&tag=%E6%96%87%E8%89%BA#0%2617360391";
		URL uri = new URL(url);
		URLConnection connection = uri.openConnection();
		InputStream in = connection.getInputStream();
		
		
		

	}
}
